package cn.zjyj.module.zjyj.service.deviceinstanceacceptancerecord;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.zjyj.module.zjyj.controller.admin.deviceinstanceacceptancerecord.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.deviceinstanceacceptancerecord.DeviceInstanceAcceptanceRecordDO;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;

import cn.zjyj.module.zjyj.dal.mysql.deviceinstanceacceptancerecord.DeviceInstanceAcceptanceRecordMapper;

import static cn.zjyj.framework.common.common.exception.util.ServiceExceptionUtil.exception;
import static cn.zjyj.framework.common.common.util.collection.CollectionUtils.convertList;
import static cn.zjyj.module.zjyj.enums.ErrorCodeConstants.*;

/**
 * 设备实例验收单 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class DeviceInstanceAcceptanceRecordServiceImpl implements DeviceInstanceAcceptanceRecordService {

    @Resource
    private DeviceInstanceAcceptanceRecordMapper deviceInstanceAcceptanceRecordMapper;

    @Override
    public Long createDeviceInstanceAcceptanceRecord(DeviceInstanceAcceptanceRecordSaveReqVO createReqVO) {
        // 插入
        DeviceInstanceAcceptanceRecordDO deviceInstanceAcceptanceRecord = BeanUtils.toBean(createReqVO, DeviceInstanceAcceptanceRecordDO.class);
        deviceInstanceAcceptanceRecordMapper.insert(deviceInstanceAcceptanceRecord);
        // 返回
        return deviceInstanceAcceptanceRecord.getId();
    }

    @Override
    public void updateDeviceInstanceAcceptanceRecord(DeviceInstanceAcceptanceRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceInstanceAcceptanceRecordExists(updateReqVO.getId());
        // 更新
        DeviceInstanceAcceptanceRecordDO updateObj = BeanUtils.toBean(updateReqVO, DeviceInstanceAcceptanceRecordDO.class);
        deviceInstanceAcceptanceRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceInstanceAcceptanceRecord(Long id) {
        // 校验存在
        validateDeviceInstanceAcceptanceRecordExists(id);
        // 删除
        deviceInstanceAcceptanceRecordMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceInstanceAcceptanceRecordListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceInstanceAcceptanceRecordExists(ids);
        // 删除
        deviceInstanceAcceptanceRecordMapper.deleteByIds(ids);
        }

    private void validateDeviceInstanceAcceptanceRecordExists(List<Long> ids) {
        List<DeviceInstanceAcceptanceRecordDO> list = deviceInstanceAcceptanceRecordMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_INSTANCE_ACCEPTANCE_RECORD_NOT_EXISTS);
        }
    }

    private void validateDeviceInstanceAcceptanceRecordExists(Long id) {
        if (deviceInstanceAcceptanceRecordMapper.selectById(id) == null) {
            throw exception(DEVICE_INSTANCE_ACCEPTANCE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public DeviceInstanceAcceptanceRecordDO getDeviceInstanceAcceptanceRecord(Long id) {
        return deviceInstanceAcceptanceRecordMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceInstanceAcceptanceRecordDO> getDeviceInstanceAcceptanceRecordPage(DeviceInstanceAcceptanceRecordPageReqVO pageReqVO) {
        return deviceInstanceAcceptanceRecordMapper.selectPage(pageReqVO);
    }

}